Function
<Function Name> ( Param1, Param2,
... )
<statements>
Return <Value>
Identifies
the start of a user-defined function.
Parameters
<Function Name> Name of the function.
Param1, Param2, ... Parameters passed to the function
and should be separated by commas.
<statements> One or more executable statements inside the
function definition.
<expression> Expression which supports variables
or value.
Return Value
Returns the
value of an expression, a constant or a variable.
Remarks
- Another method
of calling a function is:
Function
<FunctionName>
Parameters <$Param1><,$Param2>...
<statements>
Return [Expression]
- Function can
also be abbreviated as ' FUNC ' or ' FN '.
- Functions must be defined towards the end of the script, i.e, after they are called from the main function. If they are defined at the beginning of the script, the IVR will straightaway execute them.
- Either Param
or Parameters keyword can be used.
- All variables
(including parameters) inside a function are local to that function.
- All functions
are local to the script file in which they are declared.
- If a default
label like ONHANGUP, ONSYSTEMERROR is not present inside a plugin script,
then it will automatically jump to the parent scripts ONHANGUP / ONSYSTEMERROR.
- You can call
another .dt or .dtx file just like a plugin and it will follow the precedence
order given below.
- The precedence
of a function call is:
- Function
in current script.
- .dtx
file in current directory.
- .dt
file in current directory.
- .exe
file in current directory.
- .dtx
file in plugins directory.
- .dt file
in plugins directory.
- .exe
file in plugins directory.
- Functions can be called recursively.
- You can declare a function in an IVR file and call that function like ivrfile.func().
- A "goto" from a function than "return", may cause problems in long time execution. A function definition should end up with a return statement.
- If you miss a return in a function, the control will carry on the execution of subsequent statements in the next function declared below that.
- An external function can be called with a fully qualified filename like d:\Bank\Accounts($acno,$pin).
- Defining a function with reserved function name will blindly be ignored. For example, a FN Mid($param) will not get called.
Example
1. Calling a function within the same script:
Function AddNum ($Num1, $Num2)
$Num3 = $Num1 + $Num2
Return $Num3
The above function AddNum adds up the variables $Num1
and $Num2 and the resulting value $Num3 is returned.
AddNum can be beckoned in the script as:
$Num4 = AddNum ($Num1, $Num2)
* In the later case the returned value is assigned
to the variable $Num4.
* By giving values 2, 4 respectively to $Num1 and $Num2, the
value of $Num3 or $Num4 would be 6.
If this function is not found in the script file then ivr
searches for it in the precedence order as given in the remarks section.
2. Calling another .dt or .dtx file just like a plugin:
Content of script file "MathOperation.dt":
MAIN:
answer 1
$a = 10
$b = 20
$c = Addition($a,$b)
hangup
goto MAIN
ONHANGUP:
hangup
goto MAIN
Content of file "Addition.dt"
param $num1, $num2
$num3 = $num1 + $num2
return $num3
3. Recursive functions
:MAIN
answer
$num = 8
$result = abs(fact($num) - sqr($num))
display $result
hangup
goto MAIN
function abs($param)
if $param < 0
return ($param * -1)
else
return $param
endif
func fact($param)
if $param > 0
return ($param * fact($param - 1))
endif
return 1
fn sqr
parameter $num
return ($num * $num)